package com.fuhao.simpleQueue;

import com.fuhao.utils.RabbitUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消费者
 * @author zhang
 */
public class consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.获取连接
        Connection connection = RabbitUtils.getConnection();

        //2.通道
        Channel channel = connection.createChannel();

        //3.获取消息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body,"UTF-8");
                System.out.println("接收消息："+msg);
            }
        };

        //监听队列
        //参数1：监听的队列名称 参数2：是否自动ack确定  参数三：消费者获取消息后的执行方法
        channel.basicConsume("simple_queue",true,consumer);

    }
}
